package org.ucn.dmaa0914.gruppe6.gui;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.text.ParseException;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import org.ucn.dmaa0914.gruppe6.Item;
import org.ucn.dmaa0914.gruppe6.ItemCtr;

import com.jgoodies.forms.layout.ColumnSpec;
import com.jgoodies.forms.layout.FormLayout;
import com.jgoodies.forms.layout.FormSpecs;
import com.jgoodies.forms.layout.RowSpec;

public class ItemView extends JPanel {
	private JTextField txtBarcode;
	private JTextField txtTitle;
	private JTextField txtPrice;

	private int itemId = -1;
	private JTextField txtCurrency;
	
	/**
	 * Create the panel.
	 */
	public ItemView() {
		setLayout(new FormLayout(new ColumnSpec[] {
				FormSpecs.RELATED_GAP_COLSPEC,
				FormSpecs.DEFAULT_COLSPEC,
				FormSpecs.RELATED_GAP_COLSPEC,
				ColumnSpec.decode("default:grow"),
				FormSpecs.RELATED_GAP_COLSPEC,},
			new RowSpec[] {
				FormSpecs.RELATED_GAP_ROWSPEC,
				FormSpecs.DEFAULT_ROWSPEC,
				FormSpecs.RELATED_GAP_ROWSPEC,
				FormSpecs.DEFAULT_ROWSPEC,
				FormSpecs.RELATED_GAP_ROWSPEC,
				FormSpecs.DEFAULT_ROWSPEC,
				FormSpecs.RELATED_GAP_ROWSPEC,
				FormSpecs.DEFAULT_ROWSPEC,
				FormSpecs.RELATED_GAP_ROWSPEC,
				FormSpecs.DEFAULT_ROWSPEC,}));
		
		JLabel lblBarcode = new JLabel("Barcode:");
		add(lblBarcode, "2, 2, right, default");
		
		txtBarcode = new JTextField();
		add(txtBarcode, "4, 2, fill, default");
		txtBarcode.setColumns(10);
		
		JLabel lblTitle = new JLabel("Title:");
		add(lblTitle, "2, 4, right, default");
		
		txtTitle = new JTextField();
		add(txtTitle, "4, 4, fill, default");
		txtTitle.setColumns(10);
		
		JLabel lblPrice = new JLabel("Price:");
		add(lblPrice, "2, 6, right, default");
		
		txtPrice = new JTextField();
		add(txtPrice, "4, 6, fill, default");
		txtPrice.setColumns(10);
		
		JButton btnSaveChanges = new JButton("Save Changes");
		btnSaveChanges.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {

				NumberFormat format = DecimalFormat.getInstance();
				try
				{
					double price = format.parse(txtPrice.getText()).doubleValue();
					String barcode = txtBarcode.getText();
					String title = txtTitle.getText();
					ItemCtr.getInstance().updateItem(itemId, barcode, title, price, txtCurrency.getText());
					MainWindow.getInstance().updateViews();
				} catch (ParseException e) {
					e.printStackTrace();
				}
				
				
			}
		});
		
		JLabel lblCurrency = new JLabel("Currency:");
		add(lblCurrency, "2, 8, right, default");
		
		txtCurrency = new JTextField();
		add(txtCurrency, "4, 8, fill, default");
		txtCurrency.setColumns(10);
		add(btnSaveChanges, "4, 10");

	}

	public void setItem(int itemId)
	{
		Item i = ItemCtr.getInstance().find(itemId);
		if( i != null )
		{
			txtBarcode.setText(i.getBarcode());
			txtTitle.setText(i.getTitle());
			txtCurrency.setText(i.getCurrency());
			txtPrice.setText(String.format("%.2f", i.getPrice()));
			this.itemId = i.getId();
		}
		
	}
}
